const innerAudioContext = wx.createInnerAudioContext();
const db = wx.cloud.database();
Page({
  data: {
    showCollect:0,
    word:null
  },

  onLoad: function (options) {
    let that = this;
    let word = options.word;
    let wordData = options.data;
    //是否显示收藏
    let showCollect = options.showCollect;
    if (null == showCollect || '' == showCollect)
    {
      showCollect=1;
    }
    this.setData({showCollect:showCollect});


    if (null != wordData && "" != wordData)
    {
      try {
        let wordJson = JSON.parse(wordData);
        this.setData({
          word: wordJson
        });
      } catch (e) {
        console.info("=====解析单词异常==========");
        this.getWrodFromDB(word);
      }
    }else
    {
      this.getWrodFromDB(word);
    }
   
  },

  //从数据库里查询单词
  getWrodFromDB: function (word) {
    let that = this;
    let dbName = "words";

    //如果有空格说明是词组
    if (word.search(" ") != -1) {
      dbName = "word_phrase";
    }

    db.collection(dbName).where({
      word: word
    }).get().then(res => {

      if (res.data.length>0)
      {
        that.setData({
          word: res.data[0]
        });
      }else
      {
        //在单词或词组中没有查到，则到词根里面看看。
        db.collection("word_root").where({
          word: word
        }).get().then(res => {
          if (res.data.length > 0) {
            that.setData({
              word: res.data[0]
            });
          } 
        });
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})